<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;

use App\Http\Controllers\Controller;


//后台登录控制器
class LoginController extends Controller
{

	//后台登录方法
    public function index()
    {

		return view('admin/login');
    }
	
	//后台登录处理
	public function check(Request $request){
		//var_dump($_POST);
        //ar_dump($request->all());
        //接受数据
        $name=$request->input("name");
        $pass=$request->input("pass");
        $code=$request->input("code");
        //判断用户名是否输入
        if($name){
            //判断密码
            if($pass){
                //验证码是否正确

                //引入验证码类
                include "../resources/code/Code.class.php";

                //实例化验证码类
                $code1=new \Code();//  \ 找到根命名空间
                //获取验证码
                $yzm=$code1->get();
               // echo $yzm;
                if($yzm==strtoupper($code)){
                    //获取数据库
                    if($data = DB::table("user")->where('name',$name)->first()){
                        //  dd($data);
                        //将数据库中密码解密
                        if(Crypt::decrypt($data->pass)==$pass){
                            $arr=array(
                                "name"=>$name,
                                "id"=>$data->id
                            );

                            session(['adminUserInfo'=>$arr]);
                            return redirect('admin');
                        }else{
                            return back()->withInput()->with("error","密码错误");
                        }

                    }else{//withInput() 闪存输入数据到 Session
                        return back()->withInput()->with("error","用户名不存在");
                    }
                }else{
                    return back()->withInput()->with("error","验证码有误");
                }



            }else{
                return back()->with("error","请输入密码");
            }

        }else{
            return back()->with("error","请输入户名");
        }


		
	}

	//验证码的方法
    public function yzm(){
        //引入验证码类
        include "../resources/code/Code.class.php";

        //实例化验证码类
        $code=new \Code();//  \ 找到根命名空间

        //生成验证码
        $code->make();

    }

    //退出
    public function logout(Request $request){
      //  $request->session()->flush();//删除 Session 内所有数据
        $request->session()->forget('adminUserInfo');
        return redirect('admin');
    }
	
	
}
?>